//
//  LMAddressPickerView.m
//  MeetCard
//
//  Created by longmin on 16/12/6.
//  Copyright © 2016年 timtian. All rights reserved.
//

#import "LMAddressPickerView.h"
#import "UIImage+Color.h"

@interface LMAddressPickerView ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property(nonatomic, strong) UIPickerView *pickerView;

@property(nonatomic, strong) UIView *containerView;

@property(nonatomic, strong) LMButton *cancelBtn;

@property(nonatomic, strong) LMButton *confirmBtn;

@property(nonatomic, strong) NSArray *addArr;

@property(nonatomic, strong) NSArray *cityArr;

@property(nonatomic, strong) NSArray *streetArr;

@end

@implementation LMAddressPickerView

#pragma mark - 生命周期

-(void)dealloc {
    NSLog(@"dealloc----LMAddressPickerView");
}

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.containerView];
        [self.containerView addSubview:self.pickerView];
        [self.containerView addSubview:self.cancelBtn];
        [self.containerView addSubview:self.confirmBtn];
        
    }
    return self;
}

#pragma mark - Event

/** 点击取消 */
-(void)cancelBtnClick {
    
    if (self.cancelBlock) {
        self.cancelBlock();
    }
    [self dismiss];
}

/** 点击确定 */
-(void)confirmBtnClick {
    
    if (self.confimBlock) {
        
        NSInteger provinceIndex = [self.pickerView selectedRowInComponent:0];
        NSString *province = self.addArr[provinceIndex][@"name"];
        NSInteger cityIndex = [self.pickerView selectedRowInComponent:1];
        NSString *city = self.cityArr[cityIndex][@"name"];
        NSInteger streetIndex = [self.pickerView selectedRowInComponent:2];
        NSString *street = self.streetArr[streetIndex];
        self.confimBlock(province,city,street);
    }
    [self dismiss];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismiss];
}

#pragma mark - Public

+ (instancetype)addressPickerViewWithConfirmBlock:(void (^)(NSString *provinceStr,NSString *cityStr,NSString *streetStr))confirmBlock cancelBlock:(void (^)())cancelBlock {
    
    LMAddressPickerView *addressPicker = [[LMAddressPickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    addressPicker.confimBlock = confirmBlock;
    addressPicker.cancelBlock = cancelBlock;
    
    [addressPicker setLayout];
    
    return addressPicker;
}

-(void)show {
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [keyWindow addSubview:self];
    self.backgroundColor = [UIColor clearColor];
    self.containerView.top = self.height;
    
    [UIView animateWithDuration:0.36 animations:^{
        
        self.backgroundColor = kRGBACOLOR(0, 0, 0, 0.7);
        self.containerView.bottom = self.height;
        
    }];
    
}

-(void)dismiss {
    
    [UIView animateWithDuration:0.36 animations:^{
        
        self.backgroundColor = [UIColor clearColor];
        self.containerView.top = self.height;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
    
}

#pragma mark - Private

-(void)setLayout {
    
    CGFloat bottomH = 50;
    self.containerView.height = bottomH + 225 * __kwRatio;
    self.containerView.width = self.width;
    self.containerView.bottom = self.height;
    
    self.pickerView.width = self.width;
    self.pickerView.height = 225 * __kwRatio;
    self.pickerView.top = 0;
    
    CGFloat allBtnW = self.cancelBtn.width + self.confirmBtn.width + 78 * __kwRatio;
    self.cancelBtn.left = (self.containerView.width - allBtnW) * 0.5;
    self.cancelBtn.centerY = self.containerView.height - (73 * 0.5);
    
    self.confirmBtn.right = self.containerView.width - self.cancelBtn.left;
    self.confirmBtn.centerY = self.cancelBtn.centerY;
    
}

#pragma mark - 数据源和代理方法

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (0 == component)
    {
        return self.addArr.count;
    }
    if (1 == component) {
        return self.cityArr.count;

    }else{
        return self.streetArr.count;
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (0 == component)
    {
        return self.addArr[row][@"name"];
    }
    if (1 == component) {
        return self.cityArr[row][@"name"];
    }else{
        return self.streetArr[row];
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        
        self.cityArr = self.addArr[row][@"sub"];
        [pickerView reloadComponent:1];
        
        NSInteger cityIndex = [pickerView selectedRowInComponent:1];
        self.streetArr = self.cityArr[cityIndex][@"sub"];
        [pickerView reloadComponent:2];
        
    }else if(component == 1) {
        self.streetArr = self.cityArr[row][@"sub"];
        [pickerView reloadComponent:2];
    }
}

#pragma mark - UIView -> 下面放继承自UIView的方法
#pragma mark - set方法和get方法

-(UIPickerView *)pickerView {
    if (_pickerView == nil) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        _pickerView.showsSelectionIndicator = YES;
    }
    return _pickerView;
}

-(LMButton *)cancelBtn {
    if (_cancelBtn == nil) {
        _cancelBtn = [LMButton lm_buttonWithType:LMButtonTypeCancel title:@"取消"];
        UIImage *d9Img = [UIImage lm_imageWithColor:kColorD9D9D9];
        [_cancelBtn setBackgroundImage:d9Img forState:UIControlStateNormal];
        UIImage *cbImg = [UIImage lm_imageWithColor:kColorFFA466];
        [_cancelBtn setBackgroundImage:cbImg forState:UIControlStateHighlighted];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.width = 100;
        _cancelBtn.height = 44;
    }
    return _cancelBtn;
}

-(LMButton *)confirmBtn {
    if (_confirmBtn == nil) {
        _confirmBtn = [LMButton lm_buttonWithType:LMButtonTypeMove title:@"确定"];
        _confirmBtn.width = 100;
        _confirmBtn.height = 44;
        [_confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}

-(UIView *)containerView {
    if (_containerView == nil) {
        _containerView = [[UIView alloc] init];
        _containerView.backgroundColor = [UIColor whiteColor];
    }
    return _containerView;
}

-(NSArray *)addArr {
    if (_addArr == nil) {
        NSString *addressPath = [[NSBundle mainBundle] pathForResource:@"address.plist" ofType:nil];
        NSDictionary *addDic = [NSDictionary dictionaryWithContentsOfFile:addressPath];
        _addArr = addDic[@"address"];
    }
    return _addArr;
}

-(NSArray *)cityArr {
    if (_cityArr == nil) {
        _cityArr = self.addArr[0][@"sub"];
    }
    return _cityArr;
}

-(NSArray *)streetArr {
    if (_streetArr == nil) {
        _streetArr = self.cityArr[0][@"sub"];
    }
    return _streetArr;
}

@end
